package cn.lili.modules.token.extend.impl;

import cn.lili.common.security.AuthUser;
import cn.lili.common.security.enums.PermissionEnum;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.modules.store.client.StoreClient;
import cn.lili.modules.store.entity.vos.StoreVO;
import cn.lili.modules.token.extend.TokenGenerateExtend;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 商家token生成扩展
 *
 * @author Chopper
 * @version v4.0
 * @since 2020/11/16 10:51
 */
@Component
@RequiredArgsConstructor
public class StoreTokenGenerate implements TokenGenerateExtend {

    private final StoreClient storeClient;

    @Override
    public void extend(AuthUser authUser, Map<String, List<String>> userPermissions) {

        if (!authUser.getScene().equals(SceneEnums.STORE) && !authUser.getScene().equals(SceneEnums.SUPPLIER)) {
            return;
        }

        StoreVO store = storeClient.getStore(authUser.getExtendId());
        authUser.setExtendName(store.getStoreName());
        this.initPermission(userPermissions);
    }

    /**
     * 初始赋予的权限，查看权限包含首页流量统计权限，
     * 超级权限包含个人信息维护，密码修改权限
     *
     * @param userPermissions 权限对象
     */
    void initPermission(Map<String, List<String>> userPermissions) {
        List<String> superPermissions = userPermissions.get(PermissionEnum.SUPER.name());
        List<String> queryPermissions = userPermissions.get(PermissionEnum.QUERY.name());
        //TODO 用户信息维护--操作权限
        //获取当前登录用户
        superPermissions.add("/user/store*");
        superPermissions.add("/user/store/owner*");
        superPermissions.add("/user/collection*");
        superPermissions.add("/resource/upload*");
        //退出
        superPermissions.add("/auth/logout*");

        //统计查看权限
        queryPermissions.add("/statistics*");
        //菜单查看权限
        queryPermissions.add("/user/menu*");
        //商品分类查看权限
        queryPermissions.add("/goods/category*");
        //查看地区接口
        queryPermissions.add("/system/region*");

        queryPermissions.add("/resource/file");
    }

}
